#include <Windows.h>
#include <tchar.h>
#include <assert.h>
#include "resource.h"
//////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{	
	static const UINT IDC_BTNEXIT				= 1002;
	static HBITMAP hBitmap;
	switch (nMsg)
	{
		case WM_CREATE:
		{
			hBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1));
			BITMAP bmp;
			GetObject(hBitmap, sizeof(BITMAP), &bmp);
			
			//--: Move Main wnd to center of screen
			RECT rtMainWnd;
			GetClientRect(hWnd, &rtMainWnd);
			UINT nSrnWidth		= GetSystemMetrics(SM_CXFULLSCREEN);
			UINT nSrnHeight		= GetSystemMetrics(SM_CYFULLSCREEN);
			UINT nWndWidth		= bmp.bmWidth;
			UINT nWndHeight		= bmp.bmHeight;
			UINT nLeft			= (nSrnWidth - nWndWidth) / 2;
			UINT nTop			= (nSrnHeight - nWndHeight) / 2;
			MoveWindow(hWnd, nLeft, nTop, nWndWidth, nWndHeight, TRUE);
			
			CreateWindow(_T("Button"), _T("Transparent Wnd"), WS_CHILD|WS_VISIBLE, 0, 0, 150, 50, hWnd, (HMENU)IDC_BTNEXIT, NULL, NULL);

			break;
		}
		case WM_COMMAND:
		{
			switch (wParam)
			{
				case IDC_BTNEXIT:
				{	
					SetWindowLongPtr(hWnd, GWL_EXSTYLE, GetWindowLongPtr(hWnd, GWL_EXSTYLE)|WS_EX_LAYERED);
				 
					RECT rtClient;
					GetClientRect(hWnd, &rtClient);
	
					HDC hDC				= GetDC(hWnd);
					HDC hMemDC			= CreateCompatibleDC(hDC);
					HBITMAP hOldBmp		= (HBITMAP)SelectObject(hMemDC, hBitmap);

					POINT ptSrc = {0, 0};
					POINT ptPos = {rtClient.left, rtClient.top};
					SIZE szSize = {rtClient.right - rtClient.left, rtClient.bottom - rtClient.top};
					BLENDFUNCTION   blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
					UpdateLayeredWindow(hWnd, hDC, &ptPos, &szSize, hMemDC, &ptSrc, RGB(255,0,0), &blend, ULW_ALPHA);

					DWORD dwErr = GetLastError();
					SelectObject(hMemDC, hOldBmp);
					DeleteObject(hMemDC);
					ReleaseDC(hWnd, hDC);

					break;
				}
			}
			break;
		}
		case WM_PAINT:
		{
			PAINTSTRUCT pt;
			HDC hDC = BeginPaint(hWnd, &pt);
			


			EndPaint(hWnd, &pt);
			return 0;
		}
		case WM_LBUTTONDOWN:
		{
			PostMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, lParam);
			break;
		}
		case WM_DESTROY:
		{
			PostQuitMessage(0);
			break;
		}
	}

	return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
}

//////////////////////////////////////////////////////////////////////////
bool RegisterWndClass(HINSTANCE hInstance, const TCHAR* szClsName)
{
	WNDCLASSEX wcs		= {0};
	wcs.cbSize			= sizeof(WNDCLASSEX);
	wcs.hbrBackground	= (HBRUSH)GetStockObject(COLOR_APPWORKSPACE);
	wcs.lpszClassName	= szClsName;
	wcs.lpfnWndProc		= WndProc;
	wcs.hCursor			= LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
	wcs.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	wcs.style			= CS_VREDRAW|CS_HREDRAW;
	
	return (0 != RegisterClassEx(&wcs));
}

//////////////////////////////////////////////////////////////////////////
bool CreateMainWnd(HINSTANCE hInstance, const TCHAR* szClsName, const TCHAR* szWndCaption)
{
	if (NULL != szClsName && NULL != szWndCaption)
	{
		DWORD dwStyleEx = WS_EX_LEFT|WS_EX_LTRREADING|WS_EX_TOPMOST|WS_EX_TOOLWINDOW;
		DWORD dwStyle   = WS_POPUP|WS_VISIBLE|WS_CLIPSIBLINGS|WS_SYSMENU;
		HWND hWnd = CreateWindowEx(dwStyleEx, szClsName, szWndCaption, dwStyle, 0, 0, 930, 100, NULL, NULL, hInstance, NULL);
	
		if (NULL != hWnd && IsWindow(hWnd))
		{
			ShowWindow(hWnd, SW_SHOW);
			UpdateWindow(hWnd);
			return true;
		}
	}
	return false;
}

//////////////////////////////////////////////////////////////////////////
void RunMessageLoop()
{
	MSG Msg;
	while (true)
	{
		while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
		{
			if (WM_QUIT == Msg.message)
			{
				return;
			}

			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
	}
}
//////////////////////////////////////////////////////////////////////////
BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	const TCHAR* szClsName		= _T("Win32App");
	const TCHAR* szWndCaption	= _T("Demo0046");

	if (RegisterWndClass(hInstance, szClsName))
	{
		if (CreateMainWnd(hInstance, szClsName, szWndCaption))
		{
			RunMessageLoop();
		}
	}

	return TRUE;
}